package edu.pku.sei.act.taskStruct;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.dom4j.Element;

public class Role extends Ident{
	
	private Map<String, Capability> capabilities;
	private Map<String, State> states;
	private Map<String, Transition> transes;
	private Map<String, TaskMessage> taskMessages;
	
	public void addAttrisAndEles(Element element)
	{
		addAttributes(element);
		Element ele;
		ele = element.addElement(TagNames.CAPABILITIES);
		Collection<Capability> caps = capabilities.values();
		for(Capability cap : caps)
		{
			Element e = ele.addElement(TagNames.CAPABILITY);
			cap.addAttrisAndEles(e);
		}
		
		ele = element.addElement(TagNames.STATES);
		Collection<State> stats = states.values();
		for(State state : stats)
		{
			Element e = ele.addElement(TagNames.STATE);
			state.addAttrisAndEles(e);
		}
		
		ele = element.addElement(TagNames.TRANSES);
		Collection<Transition> trs = transes.values();
		for(Transition tr : trs)
		{
			Element e = ele.addElement(TagNames.TRANS);
			tr.addAttrisAndEles(e);
		}
		
		ele = element.addElement(TagNames.TASK_MESSAGES);
		Collection<TaskMessage> msgs = taskMessages.values();
		for(TaskMessage msg : msgs)
		{
			Element e = ele.addElement(TagNames.TASK_MESSAGE);
			msg.addAttrisAndEles(e);
		}
	}
		
	public Role() {
		super();
		setDefaultVal();
	}
	public Role(String id, String name)
	{
		super(id, name);
		setDefaultVal();
	}
	
	public Role(Element element) {
		super();
		setDefaultVal();
		setIdAndName(element);
		
		Element ele;
		
		ele = element.element(TagNames.CAPABILITIES);		
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.CAPABILITY);
			for(Element e : list)
			{
				Capability cap = new Capability(e);
				if(!"".equals(cap.getId()))
					capabilities.put(cap.getId(), cap);
			}					
		}
		
		ele = element.element(TagNames.STATES);		
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.STATE);
			for(Element e : list)
			{
				State state = new State(e);
				if(!"".equals(state.getId()))
					states.put(state.getId(), state);
			}					
		}
		
		ele = element.element(TagNames.TRANSES);		
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.TRANS);
			for(Element e : list)
			{
				Transition trans = new Transition(e);
				if(!"".equals(trans.getId()))
					transes.put(trans.getId(), trans);
			}					
		}
		
		ele = element.element(TagNames.TASK_MESSAGES);		
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.TASK_MESSAGE);
			for(Element e : list)
			{
				TaskMessage taskMessage = new TaskMessage(e);
				if(!"".equals(taskMessage.getId()))
					taskMessages.put(taskMessage.getId(), taskMessage);
			}					
		}
	}
	
	private void setDefaultVal()
	{
		capabilities = new TreeMap<String, Capability>();
		states = new TreeMap<String, State>();
		transes = new TreeMap<String, Transition>();
		taskMessages = new TreeMap<String, TaskMessage>();
	}
	
	public Collection<Capability> getCapabilities()
	{
		//return new TreeMap<String, Capability>(capabilities);
		return capabilities.values();
	}
	
	public Collection<State> getStates()
	{
		//return new TreeMap<String, State>(states);
		return states.values();
	}
	
	public Collection<Transition> getTranses()
	{
		//return new TreeMap<String, Transition>(transes);
		return transes.values();
	}
	
	public Collection<TaskMessage> getTaskMessages()
	{
		//return new TreeMap<String, TaskMessage>(taskMessages);
		return taskMessages.values();
	}
	
	public Capability getCapabilityById(String id)
	{
		return capabilities.get(id);
	}
	
	public State getStateById(String id)
	{
		return states.get(id);
	}
	
	public Transition getTransById(String id)
	{
		return transes.get(id);
	}
	
	public TaskMessage getTaskMessageById(String id)
	{
		return taskMessages.get(id);
	}
		
}
